import os
from typing import Optional, Dict, Any

from dotenv import load_dotenv
from pydantic_settings import BaseSettings

load_dotenv()


class DatabaseConfig(BaseSettings):
    # 数据库配置
    ASYNC_DATABASE_URL: Optional[str] = None
    DB_HOST: str = os.getenv("DB_HOST") or '127.0.0.1'
    DB_PORT: int = int(os.getenv("DB_PORT") or "3306")
    DB_USER: str = os.getenv("DB_USER") or ''
    DB_PASSWORD: str = os.getenv("DB_PASSWORD") or ''
    DB_NAME: str = os.getenv("DB_NAME") or 'abc'

    # 异步连接池配置
    ASYNC_SQLALCHEMY_ENGINE_OPTIONS: Dict[Any, Any] = {
        'pool_size': int(os.getenv("ASYNC_DB_POOL_SIZE") or "20"),
        'pool_recycle': int(os.getenv("ASYNC_DB_RECYCLE") or "3600"),
        'pool_pre_ping': (os.getenv('ASYNC_DB_POOL_PRE_PING') or "true").lower() == "true",
        'max_overflow': int(os.getenv("ASYNC_DB_MAX_OVERFLOW") or "30"),
        "pool_timeout": int(os.getenv("ASYNC_DB_TIMEOUT") or "30"),
        "echo": (os.getenv('ASYNC_DB_ECHO') or "true").lower() == "true",  # 默认开启SQL日志
        "echo_pool": (os.getenv('ASYNC_DB_ECHO_POOL') or "false").lower() == "true",  # 连接池日志
    }


mysqlConfig = DatabaseConfig()
